// pages/story/player.js
let index, video
let prePage
// let base64 = require('../../utils/base64')
import base64 from '../../utils/base64'
Page({
  onHide() {
    if (this.data.url) video.playMessage = this.data
  },
  onUnload() {
    if (this.data.url) video.playMessage = this.data
  },
  timeupdate(e) {
    this.data.currentTime = e.detail.currentTime
  },
  play(e) {
    let index = e.currentTarget.dataset.index
    let SI = this.data.sourceIndex
    let source = this.data.sources[SI]
    let video = source.list[index]
    this.setData({ currentTime: 0, jiTitle: video.title, url: video.url, ['sources[' + SI + '].list[' + index + '].selected']: true })
  },
  async onLoad(options) {
    index = options.index
    let pages = getCurrentPages()
    prePage = pages[pages.length - 2]
    let menu = prePage.data.menus[prePage.data.menuIndex]
    let type = menu.types[menu.typeIndex]
    video = type.videos[index]
    if (video.playMessage) {
      this.setData(video.playMessage)
      return
    }
    let url = 'https://miao101.com' + video.url
    let html = await this.ajax({ url })
    if (html != null) {
      let regex = /<a.+?data="([^"]+)">([^<]+)/g
      let r, list = []
      while (r = regex.exec(html)) {
        list.push({
          url: base64.decode(r[1]),
          title: r[2],
        })
      }
      regex = /<div class="text-break ft14"><p>(.+?)<\/p>/
      r = regex.exec(html)
      let jj = '简介'
      if (r) jj = r[1].replace(/<br\s*\/?>/g, '\n')
      regex = /<a class="nav-link (?:active)?" href="([^"]+)">/g
      let sources = []
      while (r = regex.exec(html)) {
        sources.push({
          url: r[1],
        })
      }
      sources[0].selected = true
      sources[0].list = list
      list[0].selected = true
      this.setData({ sources, jj, list, sourceIndex: 0, title: video.title, jiTitle: list[0].title, url: list[0].url })
    } else {
      wx.showModal({
        showCancel: false,
        content: '获取网页内容失败，请重新尝试~'
      })
    }
  },
  async sources(e) {
    let sourceIndex = e.target.dataset.index
    if (sourceIndex == undefined) return
    let change = {}
    change['sources[' + this.data.sourceIndex + '].selected'] = false
    change['sources[' + sourceIndex + '].selected'] = true
    change.sourceIndex = sourceIndex
    let source = this.data.sources[sourceIndex]
    if (!source.list) {
      let url = 'https://miao101.com' + source.url
      let html = await this.ajax({ url })
      let regex = /<a.+?data="([^"]+)">([^<]+)/g
      let r, list = []
      while (r = regex.exec(html)) {
        list.push({
          url: base64.decode(r[1]),
          title: r[2],
        })
      }
      change['sources[' + sourceIndex + '].list'] = list
    }
    this.setData(change)
  },
  ajax(e) {
    return new Promise((resolve, reject) => {
      wx.showLoading({
        title: '请稍等...',
        mask: true,
      })
      wx.request({
        url: e.url,
        timeout: e.timeout || 10000,
        complete: result => {
          wx.hideLoading()
          if (result.errMsg == "request:ok" && result.statusCode == 200) {
            resolve(result.data)
          } else {
            resolve('')
            // reject('none')
          }
        }
      })
    })
  },
})